#include "logonwidget.h"
#include "user.h"
#include "ui_logonwidget.h"
#include "maininterfacewidget.h"
#include <QHostAddress>
#include "registerwidget.h"
#include <cstring>


LogOnWidget::LogOnWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::LogOnWidget)
{
    ui->setupUi(this);
    //socket = new QTcpSocket;
}

LogOnWidget::~LogOnWidget()
{
    delete ui;
}

void LogOnWidget::on_btn_login_clicked()
{
    //get username and password
    QString username = ui->usernameLineEdit->text();
    QString password = ui->passwordLineEdit->text();
    //connect to server
    QNetworkRequest request(QUrl("http://101.43.168.188:19999/slimarveous/login"));
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
    QJsonObject json;
    json.insert("username", username);
    json.insert("password", password);
    //qDebug()<<json;
    QJsonDocument doc(json);

    QByteArray data = doc.toJson(QJsonDocument::Compact);
    QNetworkReply *reply = manager.post(request, data);
    connect(reply, &QNetworkReply::finished, [=]() {
        if (reply->error() == QNetworkReply::NoError) {
                // 读取响应数据
                QByteArray dataReply = reply->readAll();
                qDebug()<<dataReply;

                // 将 JSON 数据转换为 QVariant 对象
                QJsonParseError error;
                QJsonObject result = QJsonDocument::fromJson(dataReply, &error).object();
                // 处理 JSON 数据
                if (error.error == QJsonParseError::NoError) {
                    QVariantMap resultMap = result.toVariantMap();
                    //提取状态码
                    QVariant status_code = resultMap.value("status_code");
                    status_code.toString();
                    //提取user对应的Map
                    if(resultMap.contains("user")) {
                        QMap userMap = resultMap.value("user").toMap();
                        QString id = userMap.value("id").toString();
                        user userSelf;

                        userSelf.setUserName(username);
                        userSelf.setUserId(id);

                        //开始判断登录是否有效
                        if(status_code == "2"){
                            QMessageBox::information(this, "登录成功", "你好，"+username+"! "+"your id is " +id);

                            this->close();
                         //跳转到主界面
                            MainInterfaceWidget* mainWindow = new MainInterfaceWidget(nullptr, userSelf);
                            mainWindow->show();
                        }//登录成功

                        else{
                            QMessageBox::information(this, "登录失败", "用户名或密码有误！");
                        }//登录失败
                    }

                }

        }
        else {
            qDebug()<<"error";
        }

            // 释放资源
            reply->deleteLater();

    });

}

void LogOnWidget::on_btn_signup_clicked()
{
    this->close();

    RegisterWidget* registerWidget = new RegisterWidget();
    registerWidget->show();
}

